package com.briup.impl.client;

import com.briup.entity.Environment;
import com.briup.impl.util.BackupImpl;
import com.briup.impl.util.CopyUtil;
import com.briup.impl.util.LogImpl;
import com.briup.inter.Configuration;
import com.briup.inter.client.Gather;
import com.briup.inter.support.ConfigurationAware;
import com.briup.inter.support.PropertiesAware;
import com.briup.inter.util.Backup;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Timestamp;
import java.util.*;


public class GatherImpl implements Gather, ConfigurationAware, PropertiesAware {
    String dataFilePath;
    String backupPath;
    Backup backup;
    Logger log = Logger.getLogger(GatherImpl.class);

    @Override
    public Collection<Environment> gather() throws Exception {
        List<Environment> envs = new ArrayList<Environment>();
        // 类加载器 加载字节码的资源
        try (InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(dataFilePath);
             BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
            String line;
            // 读到文件末尾 返回null
            // 每次采集之前 先跳过备份字节长度 TODO
            long length = in.available();
            log.info("本次采集的字节长度 " + length);
            Object load = backup.load(backupPath, false);
            if(load != null){ // 第一次没有加载到
                br.skip((long) load); // 跳过指定字节
            }
            while ((line = br.readLine()) != null) {
                if(line == null || line.length() == 0){
                    continue; // 空行不读
                }
                // 100|101|2|16|1|3|5d606f7802|1|1516323597007
                // 每行就是一个Environement对象
                Environment env = new Environment();
                String[] arr = line.split("[|]");
                env.setSrcId(arr[0]); // 1
                env.setDesId(arr[1]); // 2
                env.setDevId(arr[2]); // 3
                env.setSersorAddress(arr[3]); // 4 处理
                env.setCount(Integer.parseInt(arr[4])); // 5
                env.setCmd(arr[5]); // 6
                env.setStatus(Integer.parseInt(arr[7])); // 8
                // 时间戳 -> TimeStamp
                env.setGather_date(new Timestamp(Long.parseLong(arr[8]))); // 9 // 处理
                // 和传感器地址 第四个值
                switch (env.getSersorAddress()) {
                    case "16": // 16 温度对象和湿度对象
                        env.setName("温度"); // 没有对应列
                        int wendu = Integer.parseInt(arr[6].substring(0, 4), 16);
                        env.setData((wendu * (0.00268127F)) - 46.85F); // 7 // 处理5d60 6f78 02

                        Environment env2 = CopyUtil.copy(env, "name", "data");
                        env2.setName("湿度");
                        int shidu = Integer.parseInt(arr[6].substring(4, 8), 16);
                        env2.setData(shidu);
                        envs.add(env2);
                        break;
                    case "256":
                        env.setName("光照"); // 没有对应列
                        env.setData(Integer.parseInt(arr[6].substring(0, 4), 16)); // 7 // 前2个字节
                        break;
                    case "1280":
                        env.setName("二氧化碳"); // 没有对应列
                        env.setData(Integer.parseInt(arr[6].substring(0, 4), 16)); // 7 // 前2个字节
                        break;
                }
                envs.add(env);
                // 备份度过的字节长度
                backup.store(backupPath,length,false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 遍历集合
        log.debug("采集数据: " + envs.size());
        return envs;
    }

    @Override
    public void setConfiguration(Configuration configuration) throws Exception {
        // 对象 从 configuration类获取 6个方法提供了对象
        backup = configuration.getBackup();
    }

    @Override
    public void init(Properties properties) throws Exception {
        dataFilePath = (String) properties.get("gather-file-path");
        backupPath = (String) properties.get("gather-backup-file-path");
    }
}
